package regulation.auto.com.api;

import android.os.Bundle;

import com.ptjob.basecore.bean.ClientBaseBean;
import com.ptjob.basecore.bean.ClientDataHull;
import com.ptjob.basecore.impl.ClientHttpBaseParameter;
import com.ptjob.basecore.impl.ClientHttpParameter;
import com.ptjob.basecore.impl.ClientHttpTool;
import com.ptjob.basecore.parse.ClientMainParser;
import com.ptjob.basecore.utils.LogInfo;

import regulation.auto.com.util.UserManager;

import static regulation.auto.com.util.AtuoRegulationConstants.BASE_URL;

/**
 * Created by lidongming on 2016/10/19.
 */

public class AtuoRegulationHttpApi {

//    private static final String APP_INFO_URL = UIMasterConstants.APPLICATION_URL;
//    private static final String LOCAL_APP_INFO_URL = UIMasterConstants.APPLICATION_URL;
//    private static final String HOME_INFO_URL = UIMasterConstants.HOME_URL;
//    private static final String TOP_MENU = "/menu/api";
//    private static final String HOME_INFO_URL2 = UIMasterConstants.HOME2_URL;
//    private static final String HOME_INFO_URL3 = UIMasterConstants.HOME3_URL;
//    private static final String HOME_INFO_URL4 = UIMasterConstants.HOME4_URL;
//
//    private static final String LOGIN_INFO_URL = UIMasterConstants.LOGIN_INFO_URL;
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestAppinfo(ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(APP_INFO_URL, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "demo url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestLocalAppinfo(ClientMainParser<T, D> parser) {
//        String jsonStr= TextUtil.readAssetFile(LOCAL_APP_INFO_URL);
//        LogInfo.log("geny", jsonStr);
//        return TextUtil.parserLocalString(parser, jsonStr);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestHomeInfo(ClientMainParser<T, D> parser, int id) {
//
//        Bundle params = new Bundle();
//
//        params.putString("id", String.valueOf(id));
//
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(getBaseUrl() + TOP_MENU, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//
//        LogInfo.log("geny", "demo requestHomeinfo = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestTopMenu(int id, ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//
//        params.putString("id", String.valueOf(id));
//
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(getBaseUrl() + TOP_MENU, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "requestTopMenu url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestNewList(ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(DYNAMICS_LIST_URL, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "requestNewList url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestLoginInfo(ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(LOGIN_INFO_URL, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "requestLoginInfo url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestHomeinfo2(ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(HOME_INFO_URL2, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "demo url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
//    public static <T extends ClientBaseBean, D> ClientDataHull<T> requestHomeinfo3(ClientMainParser<T, D> parser) {
//
//        Bundle params = new Bundle();
//        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(HOME_INFO_URL3, params,
//                ClientHttpParameter.Type.GET, parser, 1);
//        LogInfo.log("geny", "demo url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
//        return request(httpParameter);
//    }
//
    public static <T extends ClientBaseBean, D> ClientDataHull<T> loginRequestTask(String userName, String passWord, ClientMainParser<T, D> parser) {

        Bundle params = new Bundle();
        params.putString("grant_type", "password");
        params.putString("username", userName);
        params.putString("password", passWord);
        String url = getBaseUrl() + "/token";
        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(url, params,
                ClientHttpParameter.Type.POST, parser, 1);
        LogInfo.log("geny", "loginRequestTask url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
        return request(httpParameter);
    }

    public static <T extends ClientBaseBean, D> ClientDataHull<T> pointRequestTask(ClientMainParser<T, D> parser) {

        Bundle params = new Bundle();


        Bundle headerParams = new Bundle();

        headerParams.putString("Authorization", "bearer " + UserManager.getInstance().getAccessToken());

        String url = getBaseUrl() + "/api/points";
        ClientHttpParameter<T, D> httpParameter = new ClientHttpParameter<>(url, params,
                ClientHttpParameter.Type.GET, parser, 1);
        LogInfo.log("geny", "pointRequestTask url = " + httpParameter.getBaseUrl() + httpParameter.encodeUrl());
        httpParameter.headerParams = headerParams;

        return request(httpParameter);
    }


    public static String getBaseUrl(){
        return BASE_URL;
    }


    /**
     * 根据参数，调起请求
     */
    public static <T extends ClientBaseBean, D> ClientDataHull<T> request(
            ClientHttpBaseParameter<T, D, ?> httpParameter) {
        ClientHttpTool<T> handler = new ClientHttpTool<T>();
        return handler.requsetData(httpParameter);
    }

}
